package today.rocky.elegantcoding.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import today.rocky.elegantcoding.common.CommonResult;
import today.rocky.elegantcoding.entity.User;
import today.rocky.elegantcoding.service.UserService;

/**
 * @version 1.0
 * @vlog:
 * @description:
 * @author: Rocky
 * @createDate: 2020/6/6 18:02
 */
@RestController
@RequestMapping("/crud")
public class CrudController {

	@Autowired
	private UserService userService;

	@RequestMapping("/getUser/db/{id}")
	public CommonResult getUserByDataBase(@PathVariable("id") Integer id){
		User u = userService.getUserByIdUseDB(id);
		if (u==null){
			return CommonResult.failed("用户不存在");
		}
		return CommonResult.success(u);
	}

	@RequestMapping("/getUser/cache/{id}")
	public CommonResult getUserByCache(@PathVariable("id") Integer id){
		User u = userService.getUserByIdUseCache(id);
		if (u==null){
			return CommonResult.failed("用户不存在");
		}
		return CommonResult.success(u);
	}

	@RequestMapping("/getUser/redis/{id}")
	public CommonResult getUserByRedis(@PathVariable("id") Integer id){
		User u = userService.getUserByIdUseRedis(id);
		if (u==null){
			return CommonResult.failed("用户不存在");
		}
		return CommonResult.success(u);
	}


	@RequestMapping("/updateUser")
	public CommonResult updateUser(User user){
		int i = userService.updateUser(user);
		if(i==1){
			return CommonResult.success("success");
		}else {
			return CommonResult.failed();
		}
	}
}
